package world.shanya.serialportdemobyjava;

import android.media.MediaPlayer;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import world.shanya.serialport.SerialPort;
import world.shanya.serialport.SerialPortBuilder;
import world.shanya.serialport.discovery.DiscoveryActivity;
import world.shanya.serialport.strings.SerialPortToast;


public class MainActivity extends AppCompatActivity {

    Button Btn_conncet,Btn_disconncect,Btn_send,Btn_conncetMyDevice;
    EditText Input;
    TextView Text_recive,Text_con;
    @SuppressLint("UseSwitchCompatOrMaterialCode")

    String TAG = "imTAG";//定义常量标记


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Btn_conncet = findViewById(R.id.btn_connect);
        Btn_disconncect = findViewById(R.id.btn_disconnect);
        Btn_send = findViewById(R.id.btn_send);
        Btn_conncetMyDevice = findViewById(R.id.btn_conncetMyDevice);

        Input = findViewById(R.id.input);
        Text_con = findViewById(R.id.text_con);

        Text_recive = findViewById(R.id.text_recive);

        SerialPort serialPort = SerialPortBuilder.INSTANCE.build(this);



        //连接设备
        Btn_conncet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                serialPort.openDiscoveryActivity();


            }
        });

        //断开连接 主动
        Btn_disconncect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                serialPort.disconnect();
                Text_con.setText("连接状态：断开");
            }

        });

        Btn_conncetMyDevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //连接到传统设备
                serialPort.connectLegacyDevice("AB:F9:B7:56:34:02");

            }
        });

        //连接结果的监听
        serialPort.setConnectionResultCallback((result,bluetoothDevice)->{

            if (result == true){
                Log.i("connect result","连接结果的监听——连接成功");
                Text_con.setText("连接状态：已连接");

                //接收消息
                serialPort.setReceivedDataCallback( (data) -> {
                    Log.i("imdata","imdata--"+data);
                    Log.i("ReceivedDataCallback","接受信息: "+data);
                    Text_recive.setText(data);

//                    接收 设定消息("8","7")，弹出一个toast
                    ArrayList<String> arrayList = new ArrayList<String>();
                    if (data != null){
                        arrayList.add(data);
                        Log.i("arrayList", String.valueOf(arrayList));
//                      遍历动态数组arrayList
                        for (int i =0 ;i<arrayList.size();i++){
//                            寻找arrayList中的 "8"
                            if (arrayList.get(i).equals("8")){
                                Log.i("find8","--"+data+"--");
                                Toast.makeText(this, "8 is here", Toast.LENGTH_SHORT).show();
                            }
//                            寻找arrayList中的 "7"
                            if (arrayList.get(i).equals("7")){
                                Log.i("find7","--"+data+"--");
                                Toast.makeText(this, "7 is here", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }

                    return null;
                });


            }else if(result == false){
                Log.i("connect result","连接结果的监听——连接失败");
                Text_con.setText("连接状态：断开");

            }

            return null;
        });




                serialPort.setConnectionStatusCallback((status,bluetoothDevice)->{
                    //新连接状态接口
                    //            typealias ConnectionStatusCallback = (status: Boolean, bluetoothDevice: BluetoothDevice?) -> Unit
                    if (status == true){
                        Log.i("连接状态接口","连接状态接口t——"+status);
                    }else if (status != true){
                        Log.i("连接状态接口","连接状态接口f——"+status);
                        MediaPlayer mediaPlayer = null;
                        mediaPlayer = new MediaPlayer();
                        mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.music2);

                        mediaPlayer.start();
                    }
                    return null;
                });




    }




}